package com.xywl.escort.view.pay

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.utils.RxHelper
import kotlinx.android.synthetic.main.activity_pay_result.*
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription

@Route(path = "/xy/pay/result")
class PayResultActivity : BaseActivity() {
    @Autowired
    @JvmField
    var id = ""

    @Autowired
    @JvmField
    var orderId = ""

    @Autowired
    @JvmField
    var path = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pay_result)
        ARouter.getInstance().inject(this)
        updateViews()
    }

    private fun updateViews() {
        RxHelper.countdown(5)
                .compose(this.bindToLifecycle<Int>())
                .subscribe(object : Subscriber<Int> {

                    override fun onSubscribe(s: Subscription) {
                        s.request(java.lang.Long.MAX_VALUE)
                    }

                    override fun onNext(integer: Int?) {
                        val time = "请稍后 (" + integer + "s)"
                        text_time.text = time
                    }

                    override fun onError(e: Throwable) {
                        ARouter.getInstance()
                                .build(path)
                                .withString("id", id)
                                .withString("orderId", orderId)
                                .navigation()
                        finish()
                    }

                    override fun onComplete() {
                        ARouter.getInstance()
                                .build(path)
                                .withString("id", id)
                                .withString("orderId", orderId)
                                .navigation()
                        finish()
                    }
                })
    }

    override fun onBackPressed() {
        return
    }
}
